import os
import json
import pickle
import cv2
import threading
import random
import shutil
import numpy as np
import xml
import collections
import PIL
import sys
from PIL import Image, ImageDraw
import xml.etree.ElementTree as ET

def import_module(module, submodule=None):
    abspath = os.path.abspath(module)
    root = os.path.dirname(abspath)
    sys.path.append(root)
    module = os.path.basename(module)
    module = os.path.splitext(module)[0]
    if submodule is None:
        module = __import__(module)
        return module
    assert isinstance(submodule, str)
    module = __import__(module)
    try:
        return eval('module.%s' % submodule)
    except:
        print('Could not import %s' % (submodule))
        return module


def load_config(src):
    if not os.path.exists(src):
        print("Config file (%s) to initial UI doesn't exist." % src)
        with open('.config.json', 'w') as fp:
            config = dict()
            json.dump(config,fp)
        return config
    try:
        try:
            with open(src, encoding='utf-8') as fp:
                return json.load(fp)
        except:
            with open(src) as fp:
                return json.load(fp)
    except:
        with open('.config.json', 'w') as fp:
            config = dict()
            json.dump(config,fp)
        return config

def write_config(config, src):
    with open(src, 'w', encoding='utf-8') as fp:
        json.dump(config, fp)

def demo_fun(arg1, arg2=2, arg3=3):
    print('hello'*10)
    print(arg1,arg2,arg3)
